package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Details;
import com.xiaomi.model.Result;
import com.xiaomi.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DetailsController
 * @Description TODO
 * @Author WL
 * @Date 2021/12/1 19:34
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/details", produces = "application/json;charset=utf-8")
public class DetailsController {
    @Autowired
    private DetailsService detailsService;
    @GetMapping("/list.do")
    @ResponseBody
    public String lists(@RequestParam Integer page,
                        @RequestParam Integer limit,
                        @RequestParam(required = false) Integer orderId) {
        Map<String,Object> pars = new HashMap();
        pars.put("orderId",orderId);
        if (page !=null && limit !=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }
        List<Details> listSearch = detailsService.pageSearch(pars);
        Integer count = detailsService.getCount(pars);
        Result result = new Result();
        result.setCode(0);
        result.setCount(count);
        result.setData(listSearch);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping("/add.do")
    @ResponseBody
    public String jia(Details details){
     detailsService.insert(details);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping("/update.do")
    @ResponseBody
    public String xiu(Details details){
        detailsService.update(details);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @RequestMapping ("/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {

        detailsService.delete(id);
        Result result = new Result();
        result.setCode(0);

        String s = JSON.toJSONString(result);
        return s;
    }
}
